package com.moly.grpc.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;

public class QuickFileServer {

    public static void main(String[] args) {
        try {
            init();
            start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static ServerSocketChannel serverSocketChannel = null;
    private static Selector selector = null;

    private static void init() throws IOException {
        serverSocketChannel = ServerSocketChannel.open();
        selector = Selector.open();
        register(selector, serverSocketChannel, false, SelectionKey.OP_ACCEPT);
        serverSocketChannel.socket().bind(new InetSocketAddress(8899));

    }

    private static void start() throws IOException {

        while (true) {
            selector.select();
            Set<SelectionKey> keys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = keys.iterator();
            while (iterator.hasNext()) {

                SelectionKey key = iterator.next();
                handleKey(key);
                iterator.remove();

            }


        }


    }

    private static void handleKey(SelectionKey key) throws IOException {
        SelectableChannel channel = key.channel();
        if(channel.isOpen()){
            if (key.isAcceptable()) {
                ServerSocketChannel serverchannel = (ServerSocketChannel) key.channel();
                SocketChannel socketChannel = serverchannel.accept();

                register(selector, socketChannel, false, SelectionKey.OP_WRITE);

            }
            if (key.isReadable()) {

                System.out.println("isReadable key.isValid() " + key.isValid());

                if (key.isValid()) {
                    SocketChannel socketChannel = (SocketChannel) key.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(30);
                    socketChannel.read(buffer);
                    buffer.flip();
                    String s = new String(buffer.array(), 0, buffer.limit());

                    System.out.println("客户端发送  " + s);
                    if(s.contains("bye")){
                        key.cancel();
                        socketChannel.close();
                    }else{
                        register(selector, socketChannel, false, SelectionKey.OP_WRITE);
                    }

                } else {
                    key.cancel();
                }

            }

            if (channel.isOpen()&&key.isWritable()) {

                System.out.println("isWritable key.isValid() " + key.isValid());
                if (key.isValid()) {
                    SocketChannel socketChannel = (SocketChannel) key.channel();

                    socketChannel.write(ByteBuffer.wrap("fuwuqi  ".getBytes()));
                    register(selector, socketChannel, false, SelectionKey.OP_READ);
                } else {
                    key.cancel();
                }
            }
        }else{
            SocketAddress remoteAddress = ((SocketChannel) channel).getRemoteAddress();
            System.out.println(remoteAddress.toString()+"  duan kai e");
            System.out.println(remoteAddress+"  duan kai e");

        }



    }

    private static void register(Selector selector, SelectableChannel channel, boolean block, int ops) throws IOException {
        channel.configureBlocking(block);

        channel.register(selector, ops);

    }
}
